package com.jing.weibo.controller.info;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jing.system.comm.BaseController;
import com.jing.weibo.pojo.info.InfoType;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.info.InfoTypeService;

/**
 * 信息类型的Controller
 * @author jing.yue
 * @version 2012/09/29 1.0.0
 */
@Controller
public class InfoTypeController extends BaseController {

	private static final Logger logger = Logger.getLogger(InfoTypeController.class);

	@Autowired
	private InfoTypeService infoTypeService;

	/**
	 * 添加信息类型
	 * @return
	 */
	@RequestMapping(value = "/info/json/addInfoType")
	@ResponseBody
	public ModelMap addInfoType(HttpServletRequest request, InfoType infoType) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			result = ERROR_NOT_ADMIN;
		}
		else {
			try {
				infoType.setAdduser(userInfo.getUsername());
				infoTypeService.saveInfoType(infoType);
				result = SUCCESS;
			} catch (Exception e) {
				logger.error(userInfo.getNickname() + ": 添加信息类型异常!", e);
				result = ERROR;
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 修改信息类型
	 * @return
	 */
	@RequestMapping(value = "/info/json/updateInfoType")
	@ResponseBody
	public ModelMap updateInfoType(HttpServletRequest request, InfoType infoType) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			result = ERROR_NOT_ADMIN;
		}
		else {
			try {
				infoTypeService.updateInfoType(infoType);
				result = SUCCESS;
			} catch (Exception e) {
				logger.error(userInfo.getNickname() + ": 修改信息类型异常!", e);
				result = ERROR;
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 跳转到信息类型管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toInfoTypeManager")
	public String toInfoTypeManager(HttpServletRequest request, ModelMap modelMap) {
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			return ERROR_ADMIN;
		}
		return "admin/info/infotype_manager";
	}

	/**
	 * 查找一级信息类型
	 * @return
	 */
	@RequestMapping(value = "/info/json/findParentInfoType")
	@ResponseBody
	public ModelMap findParentInfoType(HttpServletRequest request) {
		String result = null;
		List<InfoType> infoTypeList = null;
		try {
			//获取一级信息类型
			infoTypeList = infoTypeService.findParentInfoType();
			result = SUCCESS;
		} catch (Exception e) {
			logger.error("获取一级信息类型异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("data", infoTypeList);
		return modelMap;
	}

	/**
	 * 查找信息类型
	 * @return
	 */
	@RequestMapping(value = "/info/json/findInfoType")
	@ResponseBody
	public ModelMap findInfoType(HttpServletRequest request, InfoType infoType) {
		String result = null;
		List<InfoType> infoTypeList = null;
		try {
			//获取信息类型
			infoTypeList = infoTypeService.findAllInfoType();
			result = SUCCESS;
		} catch (Exception e) {
			logger.error("获取信息类型异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("data", infoTypeList);
		return modelMap;
	}

}